package com.ldgame.gfx.gfxelements;

import java.util.ArrayList;
import org.lwjgl.util.vector.Vector2f;
import org.newdawn.slick.Color;

import com.ldgame.gfx.Screen;

public class GfxComposition implements GfxComponent {
	ArrayList<GfxComponent> components = new ArrayList<GfxComponent>();
	Vector2f position = new Vector2f();
	float width;
	float height;
	Color color;
	
	private float getLeft() {
		float left = Screen.getWidth();
		
		for (GfxComponent component : components) {
			float newLeft = component.getPosition().x - 0.5f * component.getWidth();
			if (newLeft < left) {
				left = newLeft;
			}
		}
		
		return left;
	}
	
	private float getRight() {
		float right = 0.0f;
		
		for (GfxComponent component : components) {
			float newRight = component.getPosition().x + 0.5f * component.getWidth();
			if (newRight > right) {
				right = newRight;
			}
		}
		
		return right;
	}
	
	private float getTop() {
		float top = Screen.getHeight();
		
		for (GfxComponent component : components) {
			float newTop = component.getPosition().y - 0.5f * component.getHeight();
			if (newTop < top) {
				top = newTop;
			}
		}
		
		return top;
	}
	
	private float getBottom() {
		float bottom = 0.0f;
		
		for (GfxComponent component : components) {
			float newBottom = component.getPosition().y + 0.5f * component.getHeight();
			if (newBottom > bottom) {
				bottom = newBottom;
			}
		}
		
		return bottom;
	}
	
	@Override
	public Vector2f getPosition() {
		position.set(0.0f, 0.0f);
		
		for (GfxComponent component : components) {
			position.x += component.getPosition().x;
			position.y += component.getPosition().y;
		}
		
		position.x /= (float)components.size();
		position.y /= (float)components.size();
		
		return position;
	}

	@Override
	public float getWidth() {
		float left = getLeft();
		float right = getRight();
		
		return right - left;
	}

	@Override
	public float getHeight() {
		float bottom = getBottom();
		float top = getTop();
		
		return bottom - top;
	}

	@Override
	public void render() {
		for (GfxComponent component : components) {
			component.render();
		}
	}

	@Override
	public boolean add(GfxComponent component) {
		components.add(component);
		return true;
	}

	@Override
	public boolean remove(GfxComponent component) {
		return components.remove(component);
	}

	@Override
	public GfxComponent getChild(int child) {
		GfxComponent component;
		try {
			component = components.get(child);
		} catch (IndexOutOfBoundsException e) {
			e.printStackTrace();
			component = null;
		}
		return component;
	}

	@Override
	public ArrayList<GfxComponent> getComponents() {
		return components;
	}

	@Override
	public void setPosition(Vector2f position) {
		this.position = position;
	}

	@Override
	public void setWidth(float width) {
		this.width = width;
	}

	@Override
	public void setHeight(float height) {
		this.height = height;
	}

	@Override
	public Color getColor() {
		return color;
	}

	@Override
	public void setColor(Color color) {
		this.color = color;		
	}
}
